"use strict"

const Service = require('egg').Service

class IndexService extends Service {

    /**
     * 
     * @param {首页} param 
     */
    async index(param) {
        const { app, ctx } = this
        try{
            // 广告列表(展示start = 1)
            const adver_list = await app.model.Adver.findAll({
                where: { start: 1 }
            })
            // 全部商品
            const goods_list = await app.model.Goods.findAndCountAll({
                limit: Number(param.rows),
                offset: (param.page - 1) * param.rows,
                distinct: true,
                attributes: ['id', 'title', 'price', 'img']
            })
            // 新品
            const new_goods = await app.model.Goods.findAll({
                where: { new: 1},
                limit: 4,
                attributes: ['id', 'title', 'price', 'img'],
                order: [['id', 'DESC']]
            })
            const result = {
                adver_list,
                new_goods,
                goods_list: goods_list.rows
            }
            return ctx.app.returnHint(result, '查询成功').definedSucceed
        }catch(err) {
            return ctx.app.returnHint(err, '查询失败').definedError
        }
    }
}

module.exports = IndexService